package top.lyjwn.todo.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件工具
 * 
 * @author luoyujie
 *
 */
public class FileUilt {

	private final Logger log = LoggerFactory.getLogger("info");

	private String directory;

	public FileUilt(String directory) {
		this.directory = directory; // + "/log/";
		File file = new File(this.directory);
		if (!file.isDirectory()) {
			file.mkdirs();
		}
	}

	public List<String> getFileList() {
		List<String> paths = getFileList(this.directory, new ArrayList<String>());
		return paths;
	}

	private List<String> getFileList(String directory, List<String> paths) {
		File file = new File(directory);

		if (file.exists()) {
			File[] files = file.listFiles();
			if (null == files || files.length == 0) {
				log.debug("文件夹是空的!");
				return new ArrayList<String>();
			} else {
				for (File file2 : files) {
					if (file2.isDirectory()) {
						log.info("文件夹:" + file2.getAbsolutePath());
						paths.addAll(getFileList(file2.getAbsolutePath(), paths));
					} else {
						log.info("文件:" + file2.getAbsolutePath());
						paths.add(file2.getAbsolutePath());
					}
				}
			}
		} else {
			log.info("文件不存在!");
		}
		return paths;

	}

	/**
	 * 写入文件，写入一个信息列表
	 * 
	 * @param infos
	 * @param fileName
	 * @return
	 */
	public boolean write(List<String> infos, String fileName) {
		File file = new File(this.directory + fileName);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(file, true);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
			for (String info : infos) {
				bw.write(info);
				bw.newLine();
			}
			bw.close();
			return true;
		} catch (FileNotFoundException e) {
			log.error(e.getMessage());
			return false;
		} catch (IOException e) {
			log.error(e.getMessage());
			return false;
		}

	}

	/**
	 * 写入文件，写入一行信息
	 * 
	 * @param info
	 * @param fileName
	 * @return
	 */
	public boolean write(String info, String fileName) {
		File file = new File(this.directory + fileName);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(file, true);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(info);
			bw.newLine();
			bw.close();
			return true;
		} catch (FileNotFoundException e) {
			log.error(e.getMessage() + ":==" + file.getPath());
			return false;
		} catch (IOException e) {
			log.error(e.getMessage() + ":==" + file.getPath());
			return false;
		}

	}

	public static void main(String[] args) {
		FileUilt fileUilt = new FileUilt(System.getProperty("user.dir"));
		fileUilt.getFileList();
	}

}
